<template>
  <el-dialog
    :title="!dataForm.id ? '新增字典数据' : '修改字典数据'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" label-width="80px">
      <el-form-item label="类型" prop="type">
        <el-select v-model="dataForm.type" placeholder="请选择客户类型">
            <el-option v-for="(type_val, type_key) in typeList" :key="type_key" :label="type_val" :value="type_key"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="显示名称" prop="label">
        <el-input v-model="dataForm.label" placeholder="显示名称"></el-input>
      </el-form-item>
      <el-form-item label="值" prop="value">
        <el-input v-model="dataForm.value" placeholder="显示值" :disabled="disabled"></el-input>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        disabled: false,
        dataForm: {
          id: 0,
          label: '',
          value: '',
          type: ''
        },
        typeList: {
            'customer_visit_type': '交流方式',
            'customer_type': '客户类型',
            'customer_source': '客户来源',
            'customer_level': '客户等级',
            'customer_industry': '所属行业',
        },
        dataRule: {
          type: [
            { required: true, message: '类型不能为空', trigger: 'blur' }
          ],
          label: [
            { required: true, message: '显示名称不能为空', trigger: 'blur' }
          ],
          value: [
            { required: true, message: '值不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.disabled = false
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/sys/dict/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.disabled = true
                this.dataForm.type = data.dict.type
                this.dataForm.label = data.dict.label
                this.dataForm.value = data.dict.value
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/sys/dict/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'type': this.dataForm.type,
                'label': this.dataForm.label,
                'value': this.dataForm.value
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            }).catch((error) => {
                this.$message.error('请求异常')
            })
          }
        })
      }
    }
  }
</script>
